package com.example.origin.finance.trade.dto.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.origin.finance.dto.XueqiuKline;
import com.example.origin.finance.trade.dto.Analyst;
import com.example.origin.finance.trade.dto.Conclusion;
import com.example.origin.finance.trade.dto.InfoResource;
import com.example.origin.finance.trade.dto.TradeRecord;
import com.example.origin.finance.trade.enums.Action;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class RedAnalyst extends Analyst {
    @Override
    protected Conclusion conclusion() {
        InfoResource infoResource = getInfoResource();
        String code = infoResource.getCode();
        Date time = infoResource.getTime();
        XueqiuKline xueqiuKline = infoResource.getKline();
        if (!getTradeRecordMap().containsKey(code)||getTradeRecordMap().get(code).stream().allMatch(TradeRecord::isSold)) {
            return new Conclusion(infoResource, buyFlag() && klineFlag() ? Action.BUY : Action.NONE);
        }

        List<TradeRecord> tradeRecords = getTradeRecordMap().get(code);
        for (int i = tradeRecords.size() - 1; i >= 0; i--) {
            TradeRecord tradeRecord = tradeRecords.get(i);
            if (tradeRecord.isBought()) {
                TradeRecord bean = BeanUtil.copyProperties(tradeRecord, TradeRecord.class);
                bean.recordSell(time, xueqiuKline.getClose(), tradeRecord.getBuyVolume());
                if (bean.getProfitRate() < -0.1 || sellFlag()) {
                    return new Conclusion(infoResource, Action.SELL);
                }
            }
        }
        return new Conclusion(infoResource, Action.NONE);
    }

    public boolean buyFlag() {
        if (getMoreKlines().size() < 3) {
            return false;
        }
        if (getKline().getClose() < 2 ) {
            return false;
        }
        DateTime preMonth = DateUtil.offsetMonth(getKline().getTimestamp(), -1);
        if (DateUtil.compare(getMoreKlines().get(2).getTimestamp(),preMonth)<=0) {
            return false;
        }
        if (getKline().getPercent()>7) {
            return false;
        }
        Double avgAmount = getMoreKlines().stream().limit(3).collect(Collectors.averagingDouble(XueqiuKline::getAmount));
        if (avgAmount<1E7){
            return false;
        }
        if (getMoreKlines().get(0).getPercent() > 0 && getMoreKlines().get(1).getPercent() > 0 && getMoreKlines().get(2).getPercent() > 0
                ) {
            if (getMoreKlines().get(3).getPercent() < -6 && getMoreKlines().get(4).getPercent()<0) {
                if (getMoreKlines().get(4).getOpen()<getMoreKlines().get(0).getClose()) {
                    return true;
                }
            }
//            Double avgClose = getMoreKlines().stream().skip(3).limit(3).collect(Collectors.averagingDouble(XueqiuKline::getClose));
//            if (getKline().getClose() < avgClose) {
//                return true;
//            }
        }
        return false;
    }


    public boolean sellFlag() {
        if (getMoreKlines().size() < 2) {
            return false;
        }
        if (getMoreKlines().get(0).getPercent()<-7) {
            return true;
        }
        if (getMoreKlines().get(0).getPercent() < 0 && getMoreKlines().get(1).getPercent() < 0) {
            return true;
        }
        return false;
    }

    public boolean klineFlag() {
        if (getKline().getPe() == null || getKline().getMarket_capital() == null) {
            return false;
        }
        if (getKline().getPe() >= 10 && getKline().getPe() <= 35) {
            if (getKline().getMarket_capital() >= 1E9 && getKline().getMarket_capital() <= 2E10) {
                return true;
            }

        }
        return false;
    }

}
